package com.cat.dataStructure02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/apply-operations-to-make-all-array-elements-equal-to-zero/
 * @create 2025/10/24 09:30
 * @since JDK17
 */

public class Solution57 {
    public boolean checkArray(int[] nums, int k) {
        int n = nums.length;
        int[] diff = new int[n + 1];
        for (int i = 0; i <= n - k; i++) {
            if (diff[i] < nums[i]) {
                int t = nums[i] - diff[i];
                diff[i] += t;
                diff[i + k] -= t;
            }
            if (diff[i] > nums[i]) {
                return false;
            }
            diff[i + 1] += diff[i];
        }
        for (int i = n - k + 1; i < n; i++) {
            if (diff[i] != nums[i]) {
                return false;
            }
            diff[i + 1] += diff[i];
        }
        return true;
    }
}
